package com.test.controller;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.test.R;
import com.test.util.StringUtil;
import com.test.util.randomUtil;

import java.io.IOException;


/**
 * 计时器
 * 可随意切换格式化
 * 计时时间等
 */
public class ChronometerActivity extends AppCompatActivity implements View.OnClickListener, Chronometer.OnChronometerTickListener {

    private Chronometer chronometer;

    private Button btn_start, btn_stop, btn_reset, btn_format, btn_game;

    private long stop_time;

    private MediaPlayer mMediaPlayer;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.chronometer_demo);
        timeController();
    }

    /**
     * 写一个方法
     */
    private void timeController() {

        chronometer = findViewById(R.id.chronometer);
        btn_start = findViewById(R.id.btn_start);
        btn_stop = findViewById(R.id.btn_stop);
        btn_reset = findViewById(R.id.btn_reset);
        btn_format = findViewById(R.id.btn_format);

        //写一个点击事件玩玩儿
        btn_game = findViewById(R.id.btn_game);

        //注册监听器
        chronometer.setOnChronometerTickListener(this);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_reset.setOnClickListener(this);
        btn_format.setOnClickListener(this);
        btn_game.setOnClickListener(this);

        chronometer.setOnClickListener(this);
        playLocalFile();
    }

    /**
     * 点击事件主要业务
     */
    @Override
    public void onClick(View view) {

        //try catch 捕获异常
        try {
            switch (view.getId()) {
                case R.id.btn_start://开始计时
                    if (stop_time != 0) {
                        chronometer.setBase(chronometer.getBase() + (SystemClock.elapsedRealtime() - stop_time));
                        //显示提示 调用的 dealWithByString(chronometer) 方法
                        Toast.makeText(this, "开始时间: " + StringUtil.dealWithByString(chronometer), Toast.LENGTH_LONG).show();
                    } else {
                        chronometer.setBase(SystemClock.elapsedRealtime());
                        Toast.makeText(this, "开始时间:  " + StringUtil.dealWithByString(chronometer), Toast.LENGTH_LONG).show();
                    }
                    chronometer.start();
                    // 按钮在点击后就设置为不可再按
                    btn_start.setEnabled(false);
                    break;

                case R.id.btn_stop: //停止计时
                    chronometer.stop();
                    btn_start.setEnabled(true);
                    stop_time = SystemClock.elapsedRealtime();

                    //写一个随机数  点击停止时间提示不同数字
                    int num = randomUtil.randomTree();
                    if (num == 1) {
                        Toast.makeText(this, " 主人时间到了唷 ~ ", Toast.LENGTH_LONG).show();
                    }
                    if (num == 2) {
                        Toast.makeText(this, " 谢谢主人 ~ ", Toast.LENGTH_LONG).show();
                    }
                    if (num == 3) {
                        Toast.makeText(this, " 时间都去哪儿了 ~ ", Toast.LENGTH_LONG).show();
                    }
                    break;

                case R.id.btn_reset:
                    chronometer.setBase(SystemClock.elapsedRealtime());//重置时间

                    if (randomUtil.random() != 11) {
                        // 音乐重置
                        mMediaPlayer.reset();
                        // 重新加载音乐
                        playLocalFile();
                    } else {
                        mMediaPlayer.stop();
                    }
                    break;

                case R.id.btn_format:
                    // 判断格式化可连续点击  切换格式
                    if (chronometer.getFormat() == null || chronometer.getFormat().equals("%s")) {
                        chronometer.setFormat("计时  %s");
                    } else {
                        chronometer.setFormat("%s");
                    }
                    break;
                // 写一个点击事件小游戏玩玩
                case R.id.btn_game: {

                    int randomNum = randomUtil.random();
                    // 11就停止音乐
                    if (randomNum == 11) {
                        // 音乐重置
                        mMediaPlayer.reset();
                        mMediaPlayer.stop();

                    }
                    //写一个随机数  点击停止时间提示不同数字
                    Toast.makeText(this, StringUtil.game(randomNum), Toast.LENGTH_LONG).show();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onChronometerTick(Chronometer chronometer) {

        //        String str = chronometer.getText().toString();
        //        if (str.equals("00:00")) {
        //            Toast.makeText(this, "时间到了 ~ ", Toast.LENGTH_LONG).show();
        //        }

    }

    // 音乐不断
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // BGM播放
    private void playLocalFile() {

        try {
            // 随机音乐播放
            int num = (int) (Math.random() * (4) + 1);
            //播放bai工程res目录下的raw目录中du的音乐文件in_call_alarm
            myMediaPlayer(num);
            mMediaPlayer.prepare();
        } catch (IOException e) {

        } catch (IllegalStateException e) {

        }
        // 开始播放（线程）
        new Thread() {
            @Override
            public synchronized void run() {
                try {
                    // 开始播放
                    mMediaPlayer.start();
                    mMediaPlayer.setLooping(true);
                } catch (Exception e) {
                }
            }
        }.start();
    }


    public MediaPlayer myMediaPlayer(int num) {
        if (num == 1) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.in_call_alarm);
        }
        if (num == 2) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.in_call_alarm_one);
        }
        if (num == 3) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.in_call_alarm_two);
        }
        if (num == 4) {
            mMediaPlayer = MediaPlayer.create(this, R.raw.in_call_alarm_tree);
        }
        return mMediaPlayer;
    }


    /**
     * 测试切割main方法
     */
    public static void main(String[] args) {
        String str = "计时  00:00";
        String arr[] = str.split("  ");
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        //随机数
        int num = (int) (Math.random() * (4) + 1);
        System.out.println(num);
    }
}
